class Solution {
public:
    //const int N=2100;
    // int d[2100];
    // int n;
    vector<vector<int>> edges;
    vector<int> d;
    bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
        edges.resize(numCourses);
        d.resize(numCourses);
        for (vector<int>x : prerequisites) {
            int a = x[0];
            int b = x[1];
            //cout<<a<<endl;
            d[a]++;
            edges[b].push_back(a);
        }
        queue<int>q;
        for (int i = 0; i < numCourses; i++) {
            if (!d[i]) {
                q.push(i);
            }
        }
        int cnt = 0;
        while (q.size()) {
            int t = q.front();
            q.pop();
            cnt++;
            for (int x : edges[t]) {
                if (--d[x] == 0) {
                    q.push(x);
                }
            }
        }
        return cnt == numCourses;
    }
};